package graph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

/**
 * Class that represent structure graph
 * 
 * 		info
 * 			edge = aresta
 * 			adjacent = adjacente
 * 			vertex = vertice	
 *  
 * @author rocha
 */
public class Graph {
	
	/**
	 * structure graph
	 */
	private ArrayList<Integer>[] graph;
	
	/**
	 * @return sctructure graph
	 */
	public ArrayList<Integer>[] getGraph() {
		return graph;
	}
	
	/**
	 *  constructor default
	 *  <li>initialize graph
	 */
	@SuppressWarnings("unchecked")
	public Graph() {
		this.graph = new ArrayList[0];
	}
	
	/**
	 *  constructor that passed number of vertex  
	 *  <li>initialize graph
	 *  <li>restructured graph
	 */
	@SuppressWarnings("unchecked")
	public Graph(int numberOfvertex) {
		this.graph = new ArrayList[numberOfvertex];
	}
	
	/**
	 * @return number of the vertex
	 */
	public int getVertexNumber() {
		return graph.length;
	}
	
	/**
	 * obtained adjacents graph at the vertex orientation 
	 * @param o orientation 
	 * @return list of the vertex adjacents specified 
	 */
	public List<Integer> getAdjacents(int o) {
		return graph[o];
	}
	
	/**
	 * override to information at the total graph 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < this.graph.length; i ++) {
			builder.append("Adj[");
			builder.append(i);
			builder.append("] = ");
			builder.append(this.graph[i].toString().replace('[', '{').replace(']', '}'));
			builder.append("\n");
		}
		return builder.toString();
	}
	
	/**
	 * @return
	 */
	public String printStructureEdge() {
		StringBuilder bE = new StringBuilder();
		for (int i = 0; i < this.graph.length; i ++) {
			for (java.util.Iterator it = this.graph[i].iterator(); it.hasNext();) {
				bE.append("(" + i + "," + it.next() + ")");
				bE.append( (( i+1 == this.graph.length && !it.hasNext() ) ? "" : ", " ) ); 
			}
		}
		return "E(G)= { " + bE.toString() + " }";
	}
	
	/**
	 * @return
	 */
	public String printStructureVertex() {
		StringBuilder bV = new StringBuilder();
		for (int i = 0; i < this.graph.length; i ++) {
			bV.append(i);
			bV.append( (( i+1 == this.graph.length ) ? "" : ", " ) );
		}
		return "V(G)= { " + bV.toString() + " }";
	}
	
}